<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="renderer" content="webkit">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=10, user-scalable=yes">
	<meta name="description" content="">
	<meta name="keywords" content="">
	<link rel="shortcut icon" href="./favicon.ico"/>
	<link rel="bookmark" href="./favicon.ico"/>
	<title>中威data.true大数据可视化采集分析及文件转换系统软件V1.0</title>
	<link rel="stylesheet" href="assets/libs/bootstrap/css/bootstrap.min.css">
	<link rel="stylesheet" type="text/css" href="assets/css/reset.css">
	<link rel="stylesheet" type="text/css" href="assets/css/font-awesome-4.5.0/css/font-awesome.min.css">
	<link rel="stylesheet" type="text/css" href="assets/js/lib/handsontable/handsontable.full.min.css">
	<link rel="stylesheet" type="text/css" href="assets/css/animate.css">
	<link rel="stylesheet" type="text/css" href="assets/css/plugins.css">
	<link rel="stylesheet" type="text/css" href="assets/css/app.css">
	<style>
		td.formula {
		  background-color: yellow;
		}
		td.formula-error {
		  background-color: red;
		}
		#loading{
			position: absolute;
			left: 50%;
			top: 50%;
			width: 100px;
			height: 100px;
			margin-top: -50px;
			margin-left: -50px;
			z-index: 999999;
			color: #02B061;
			display: none;
			text-align: center;
			line-height: 100px;
			font-size: 30px;
		}
		#loading span{
			width: 100%;
			height: 100%;
			border-radius: 10px;
			background-color: rgba(200,200,200,0.6);
			position: absolute;
			left: 0;
			top: 0;
			z-index: -1;
		}
	</style>
</head>
<body>
	<div id="app" class="app-warper"></div>
	<div id="loading"><i class="fa fa-lg fa-spin fa-spinner"></i><span></span></div>
	<div id="modal_el" class="modal_el"></div>
</body>
<script src="assets/js/lib/rule/full/ruleJS.all.full.js"></script>
<script src="assets/js/lib/handsontable/handsontable.full.js"></script>
<script src="assets/js/lib/echarts.min.js"></script>
<script src="assets/js/lib/jquery.js"></script>
<script src="assets/libs/bootstrap/js/bootstrap.min.js"></script>
<script src="../dist/app.min.js"></script>
<script>
var prefix='T_CMS_DATA_';
var host='http://192.168.0.175:8082';
var actions={
    gettpl:{
        name:'读取模板',
        url:'/TrueDPS/trueDataController/getTemplateList.do?t=2016&callback=?',
        type:'all'
    },
    savetpl:{
        name:'保存模板',
        url:'/TrueDPS/trueDataController/saveTemplate.do',
        type:'all'
    },
	savechart:{
        name:'保存模板',
        url:'/TrueDPS/trueDataController/saveComponent.do',
        type:'all'
    },
    create:{
        name:'创建表',
        url:'/TrueDPS/trueDataController/creatTable.do',
        type:'all'
    },
    insert:{
        name:'插入数据',
        url:'/TrueDPS/trueDataController/saveTrueData.do',
        type:'all'
    },
    gettablelist:{
        name:'获取表列表',
        url:'/TrueDPS/trueDataController/getModelList.do?t=2016',
        type:'all'
    },
    gettable:{
        name:'获取表',
        url:'/TrueDPS/trueDataController/getFieldList.do?t=2016&modelId=',
        type:'all'
    },
    getdata:{
        name:'获取数据',
        url:'/TrueDPS/trueDataController/queryTrueData.do?t=2016&callback=?',
        type:'all'
    }
};


var admin=new Marker(host);
// var admin=new ChartMarker(host);

// 测试，二维数
var arr=[
    ['地区', '总量', '总量排名', '增幅', '增幅排名', '经度', '纬度'],
    ['南京市', 337.8, 2, 12.2, 6, 118.78, 32.04],
    ['无锡市', 274.9, 3, 7.5, 10, 120.29, 31.59],
    ['徐州市', 168.8, 5, 10.6, 9, 117.2, 34.26],
    ['常州市', 145.6, 7, 6, 12, 119.95, 31.79],
    ['苏州市', 545.4, 1, 7.2, 11, 120.62, 31.32],
    ['南通市', 189, 4, 15.7, 1, 121.05, 32.08],
    ['连云港市', 100.9, 11, 13.1, 5, 119.16, 34.59],
    ['淮安市', 120, 8, 11.9, 7, 119.15, 33.5],
    ['盐城市', 161.1, 6, 15.1, 2, 120.13, 33.38],
    ['扬州市', 107.1, 9, 14.1, 4, 119.42, 32.39],
    ['镇江市', 94.1, 12, 4.6, 13, 119.44, 32.2],
    ['泰州市', 104.1, 10, 14.3, 3, 119.9, 32.49],
    ['宿迁市', 83.7, 13, 11.2, 8, 118.3, 33.96]
];
var arr1=[
    ['地区', '总量', '总量排名', '增幅', '增幅排名', '经度', '纬度'],
    ['南京市', 337.8, 2, 12.2, 6, 118.78, 32.04]
];
var arr2=[
    ['地区', '总量', '总量排名', '增幅', '增幅排名', '经度', '纬度'],
    ['连云港市', 100.9, 11, 13.1, 5, 119.16, 34.59]
];
var data1=arrayToObj(arr1), data2=arrayToObj(arr2);
var file=tools.generateSheets('名称',null,'items','临时表',2,data1.r,data1.c,null,null,data1.d);
var sheet=tools.extend({},file.sheets[0]);
sheet.id=tools.uuid();
sheet.data=data2.d
file.sheets.push(sheet);
admin.open(null,0);

var _options=ChartParser.options();
var _group={"id":"7B0B576D-47D2-4B16-9FF6-E4A6B985649D","name":"g1","r":0,"c":0,"lr":13,"lc":6,"type":"bar","legend":[0,1,0,4],"category":[1,0,13,0],"value":[1,1,13,4]};


admin.hooks.addHook('savetpl',function(o){
    var data={
        id:o.params.id,
        name:o.params.name,
        summary:o.params.desc,
        content:JSON.stringify(o.params)
    };
    console.log('savetpl',data);
    $.post(host+actions.savetpl.url,{data:JSON.stringify(data)},function(result){
        alert(result.data||(result.error.code+':'+result.error.text));
    });
});
admin.hooks.addHook('savedata',function(o){
    var file=o.params;
    var fields=[];
    file.sheets.forEach(function(e){
        var colheader=e.colheader;
        colheader.forEach(function(e1){
            fields.push(e1.dbopts.field);
        });
        var rows=toArray(e.data,colheader[0].r||0);
        var data={
                table:prefix+file.dbname,
                fields:fields,
                rows:rows,
                count:rows.length
            };
        $.post(host+actions.insert.url,{data:JSON.stringify(data)},function(result){
            alert(result.data||(result.error.code+':'+result.error.text));
        });
    });
});
admin.hooks.addHook('todb',function(o){
    let db=o.params;
    // console.log('todb',db);
    $.post(host+actions.create.url,{data:JSON.stringify(db)},function(result){
        alert(result.data||(result.error.code+':'+result.error.text));
        if(result.error===null){
            admin.updatestate();
        }
    });
});
admin.hooks.addHook('opendbtable',function(o){
    var params=o.params;
    var query='&table='+params.table+'&start='+params.start+'&size='+params.size+'&order='+params.order;
    $.getJSON(host+actions.getdata.url+query,function(result){
        // console.log('opendbtable',result);
        if(result.error===null){
            var r=result.data.rows.length,c=result.data.fields.length;
            c=c>26?c:26;
            var data={}, cols=[];
            result.data.fields.forEach(function(e,i){
                data[i]=tools.newcell(0,i,null,e.note,e.name,true);
            });

            result.data.rows.forEach(function(arr,r){
                r=r+1;
                arr.forEach(function(arr2,i){
                    var id=r*c+i;
                    data[id]=tools.newcell(r,c,null,arr2);
                });
            });
            var file=tools.generateSheets('临时模板','','items','临时模板仅有数据显示功能', 2, r, c, null,null, data);
            admin.open(file,2);
        }
    });
});
admin.hooks.addHook('savechart',function(o){
	var d=o.params.data;
	if(o.params.evt==='savechart'){
		var data={
			id:d.id,
			name:d.name,
			tag:d.desc,
			content:d.data
		};
		$.post(host+actions.savechart.url,{data:JSON.stringify(data)},function(result){
			alert(result.data||(result.error.code+':'+result.error.text));
		});
	}else if(o.params.evt==='savegroup'){
        var cdata=parseData(arr,d);
        console.log(cdata,JSON.stringify(cdata));
	}
});

function toArray(data,idx){
    var data_arr=[],rows=[],cols=[],_r=0;
    for(var i in data){
        if(data[i].r>idx){
            data_arr.push({i:i, r:data[i].r, c:data[i].c, v:data[i].value.toString()});
        }
    }
    data_arr.sort(function(a,b){return a.i-b.i});
    data_arr.forEach(function(e){
        if(e.r!==_r){
            if(cols.length>0) rows.push(cols.map(function(e){return e;}));
            cols=[];
            _r=e.r;
        }
        cols.push(e.v);
    });
    rows.push(cols);
    return rows;
}


function arrayToObj(arr){
    var r=arr.length,c=arr[0].length;
    var cols=c>26?c:26;
    var data={};
    for(var i=0;i<r;i++){
        for(var m=0;m<c;m++){
            var id=i*cols+m;
            data[id]=tools.newcell(i,m,null,arr[i][m]);
        }
    }
    return {r:r>30?r:30,c:cols,d:data};
}

// 处理图表数据
function parseData(data,group){
    var option=_options[group.type];
    var cdata={};
    option.base.forEach(function(e){
        if(!!group[e]){
            var d=getCells(data,group[e],e);
            cdata[e]={direction:d[0],value:d[1]};
        }
    }); 
    var parse=ChartParser[group.type];
    if(typeof parse==='function'){
        return parse(cdata);
    }
    return null;
}

function getCells(data,sels,type){
    var values=[];
    var direction='',ld=0,cd=0;
    var r=sels[0],c=sels[1],lr=sels[2],lc=sels[3];
    for(;r<=lr;r++){
        for(var _c=c;_c<=lc;_c++){
            var d=data[r][_c];
            if(d==null){
                if(type=='value'){
                    values.push({r:r,c:_c,value:null});
                }
            }else{
                values.push({r:r,c:_c,value:d});
            }
        }
    }
    if(lr-r<lc-c){
        direction='left-right';
    }else if(lr-r>lc-c){
        direction='top-bottom';
    }else{
        direction='';
    }
    return [direction, values];
}
</script>
</html>